package com.thread.lianxi;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

public class PrizeDraw {
	
	private Object obj;

	public static void main(String[] args) throws InterruptedException {

		Runnable dt = new DrawThread();

		new Thread(dt, "抽奖1").start();

		new Thread(dt, "抽奖2").start();
	}

}

class DrawThread implements Runnable {

	final ReentrantLock lock = new ReentrantLock();
	// private final java.util.concurrent.locks.Condition condition =
	// lock.newCondition();

	final int[] arr = { 10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300 };
	List<Integer> list = new ArrayList<>();

	@Override
	public void run() {
		while (true) {
			try {
				lock.lock();
				System.out.println(Thread.currentThread().getName()+"开始抽取");
				int index = (new Random()).nextInt(arr.length - 1);
				if (list.contains(index)) {
					System.out.println(index+"已经被抽取过了！");
					continue;
				}else{
					list.add(index);
					int x = arr[index];
					System.out.println(Thread.currentThread().getName() + " 又产生了一个" + x + "元大奖");
				}
			} finally {
				lock.unlock();
			}
		}
	}

}
